//
//  RegisterViewController.m
//  MarianasTrench
//
//  Created by wangjue on 16/3/25.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()<UITextFieldDelegate>
//用户
@property (nonatomic, retain) UITextField *userField;
@property (nonatomic, retain) UILabel *userLabel;
//密码
@property (nonatomic, retain) UITextField *passWordField;
@property (nonatomic, retain) UILabel *passWordLabel;
//注册
@property (nonatomic, retain) UIButton *registerButton;
//登录
@property (nonatomic, retain) UIButton *enterButton;
//修改密码
@property (nonatomic, retain) UIButton *changeButton;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor = [UIColor colorWithRed:0.87 green:0.86 blue:0.87 alpha:1.00];
    
    //返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(5, 10, 50, 50);
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"<返回" forState:UIControlStateNormal];
    [self.view addSubview:backButton];
    
    
    
    //判断是否已经登录
    BmobUser *bUser = [BmobUser getCurrentObject];
    if (bUser) {
        //进行操作
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, self.view.frame.size.height / 2 - 180, 100, 100)];
        imageView.layer.cornerRadius = 50;
        imageView.layer.masksToBounds = YES;
        imageView.layer.borderWidth = 1;
        imageView.image = [UIImage imageNamed:@"c8177f3e6709c93d16cb6adb983df8dcd10054e4.jpg"];
        [self.view addSubview:imageView];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 2 - 70, self.view.frame.size.width, 20)];
        label.text = bUser.username;
        label.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:label];
        
        
        
        
    }else{
        //对象为空时，可打开用户注册界面
        //帐号
        self.userField = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 100, self.view.frame.size.height / 2 - 150, 200, 30)];
        _userField.delegate = self;
        _userField.placeholder = @"请输入用户名";
        _userField.layer.cornerRadius = 15;
        _userField.layer.borderWidth = 1;
        _userField.layer.borderColor = [UIColor grayColor].CGColor;
        _userField.clearsOnBeginEditing = YES;
        _userField.enabled = YES;
        [self.view addSubview:_userField];
        
        self.userLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 160, self.view.frame.size.height / 2 - 150, 100, 30)];
        _userLabel.text = @"用户名:";
        _userLabel.font = [UIFont systemFontOfSize:17];
        [self.view addSubview:_userLabel];
        
        
        
        
        //密码
        self.passWordField = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 100, self.view.frame.size.height / 2 - 100, 200, 30)];
        _passWordField.placeholder = @"请输入密码";
        _passWordField.delegate = self;
        _passWordField.secureTextEntry = YES;
        _passWordField.layer.borderWidth = 1;
        _passWordField.layer.borderColor = [UIColor grayColor].CGColor;
        _passWordField.layer.cornerRadius = 15;
        [self.view addSubview:_passWordField];
        
        self.passWordLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 160, self.view.frame.size.height / 2 - 100, 100, 30)];
        _passWordLabel.text = @"  密码:";
        _passWordLabel.font = [UIFont systemFontOfSize:17];
        // _passWordLabel.textAlignment = NSTextAlignmentRight;
        [self.view addSubview:_passWordLabel];
        
        
        

    }
    
       //注册按钮
    self.registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _registerButton.frame = CGRectMake(self.view.frame.size.width / 2 - 100, self.view.frame.size.height / 2 - 50, 50, 50);
    _registerButton.layer.cornerRadius = 20;
    [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
    _registerButton.backgroundColor = [UIColor clearColor];
    _registerButton.layer.borderWidth = 1;
    _registerButton.layer.borderColor = [UIColor grayColor].CGColor;
    [_registerButton addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registerButton];
    
    //登录按钮
    self.enterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _enterButton.frame = CGRectMake(self.view.frame.size.width / 2 - 100 + 200 - 50, self.view.frame.size.height / 2 - 50, 50, 50);
    _enterButton.layer.cornerRadius = 20;
    _enterButton.layer.borderWidth = 1;
    _enterButton.layer.borderColor = [UIColor grayColor].CGColor;
    [_enterButton setTitle:@"登录" forState:UIControlStateNormal];
    _enterButton.backgroundColor = [UIColor clearColor];
    [_enterButton addTarget:self action:@selector(enterAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_enterButton];
    
    //修改密码按钮
    self.changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _changeButton.frame = CGRectMake(self.view.frame.size.width / 2 - 40, self.view.frame.size.height / 2 - 50, 80, 50);
    [_changeButton addTarget:self action:@selector(changeAction:) forControlEvents:UIControlEventTouchUpInside];
    [_changeButton setTitle:@"修改密码" forState:UIControlStateNormal];
    _changeButton.layer.cornerRadius = 20;
    _changeButton.layer.borderWidth = 1;
    _changeButton.layer.borderColor = [UIColor grayColor].CGColor;
    [self.view addSubview:_changeButton];
    
    
    
    
    
}

//修改密码
-(void)changeAction:(UIButton *)button{
    
    BmobUser *bUser = [BmobUser getCurrentObject];
    if (bUser) {
        //进行操作
        ChangeViewController *VC = [[ChangeViewController alloc] init];
        
        VC.str = bUser.username;
        [self presentViewController:VC animated:YES completion:nil];
    }else{
        //对象为空时，可打开用户注册界面
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"修改密码提示" message:@"请先登录才能修改密码" preferredStyle:UIAlertControllerStyleAlert];
        
        
        
        UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
         
        }];
        
        [alert addAction:alertAction1];
        [self presentViewController:alert animated:YES completion:nil];

    }

    
    
   
}


-(void)backAction:(UIButton *)button{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

-(void)registerAction:(UIButton *)button{
   
    RegisterNextViewController *VC = [[RegisterNextViewController alloc] init];
    [self presentViewController:VC animated:YES completion:^{
        
    }];
    
    
   }

-(void)enterAction:(UIButton *)button{
    
    BmobUser *bUser = [BmobUser getCurrentObject];
    if (bUser) {
        //进行操作
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录提示" message:@"您已经登录了，如果需要切换帐号，请先注销" preferredStyle:UIAlertControllerStyleAlert];
        
        
        
        UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:alertAction1];
        [self presentViewController:alert animated:YES completion:nil];
        
        
    }else{
        //对象为空时，可打开用户注册界面
        
        [BmobUser loginWithUsernameInBackground:_userField.text password:_passWordField.text block:^(BmobUser *user, NSError *error) {
            
            if (error) {
                
                if ([_userField.text isEqualToString:@""] || [_passWordField.text isEqualToString:@""] ) {
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录提示" message:@"请输入帐号或密码" preferredStyle:UIAlertControllerStyleAlert];
                    
                    
                    
                    UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                    }];
                    
                    [alert addAction:alertAction1];
                    [self presentViewController:alert animated:YES completion:nil];
                } else {
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录提示" message:@"帐户或密码不对" preferredStyle:UIAlertControllerStyleAlert];
                    
                    
                    
                    UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                    }];
                    
                    [alert addAction:alertAction1];
                    [self presentViewController:alert animated:YES completion:nil];
                }
                
                
                
                
                
                
                
                
            }else{
                
                
                [self.delegate userName:_userField.text];
                
                [self dismissViewControllerAnimated:YES completion:nil];
                
                
                
                
                
            }
            
            
        }];

    }
    
    
    

    
    
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
